/*
#include "stdafx.h"
#include "Record.h"

#ifdef WIN32
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif //_DEBUG
#endif //WIND32
*/

namespace serika
{
	namespace dq
	{
		template<class Type>
		Record<Type>::Record(Type* type, TransientState::Enum state)
			: mState(state)
			, mDb(type)
		{
		}

		template<class Type>
		Record<Type>::~Record()
		{
			if (mDb)
			{
				delete mDb;
			}
		}

		template<class Type>
		void Record<Type>::Save()
		{
			if (this->IsNew() || this->IsDirty())
			{
				mDb->Save();
				this->ToCleanState();
			}
		}

		template<class Type>
		void Record<Type>::Delete()
		{
			if (this->IsDelete())
			{
				mDb->Destroy();
			}
		}

		template<class Type>
		void Record<Type>::ToCleanState()
		{
			if (TransientState::New == mState ||
				TransientState::Dirty == mState)
			{
				mState = TransientState::Clean;
			}
		}

		template<class Type>
		void Record<Type>::ToDeleteState()
		{
			if (TransientState::Dirty == mState ||
				TransientState::Clean == mState)
			{
				mState = TransientState::Delete;
			}
		}

		template<class Type>
		void Record<Type>::ToDirtyState()
		{
			if (TransientState::Clean == mState)
			{
				mState = TransientState::Dirty;
			}
		}

		template<class Type>
		int Record<Type>::GetInt(const std::string& col, int def /* = 0 */) const
		{
			if (!mDb)
			{
				return def;
			}

			try
			{
				return mDb->GetRawValue<int>(col);
			}
			catch (const db::RecordException& /*ex*/)
			{
				return def;
			}
		}

		template<class Type>
		const ut::String& Record<Type>::GetString(const std::string& col, const ut::String& def /* = */ ) const
		{
			if (!mDb)
			{
				return def;
			}

			try
			{
				return mDb->GetRawValue<ut::String>(col);
			}
			catch (const db::RecordException& /*ex*/)
			{
				return def;
			}
		}

		template<class Type>
		void Record<Type>::Set(const std::string& col, const db::Any& any)
		{
			if (!this->IsDelete() && mDb)
			{
				mDb->SetValue(col, any);
				this->ToDirtyState();
			}
		}
	}
}
